package csc.fresher.service.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.constant.ProjectBankingConstant;
import csc.fresher.dao.CurrencyDao;
import csc.fresher.dao.InterestRateDao;
import csc.fresher.dao.PeriodTypeDao;
import csc.fresher.entity.Currency;
import csc.fresher.entity.InterestRate;
import csc.fresher.entity.PeriodType;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.factory.InterestRateDaoFactory;
import csc.fresher.service.InterestRateService;

@Service
public class SavingInterestRateServiceImpl implements InterestRateService{

	@Autowired
	InterestRateDaoFactory interestRateDaoFactory;
	@Autowired
	CurrencyDao currencyDao;
	@Autowired
	PeriodTypeDao periodTypeDao;
	
	private InterestRateDao interestRateDao;
	
	@PostConstruct
	public void init() {
		interestRateDao = interestRateDaoFactory.getType(ProjectBankingConstant.SAVING_ACCOUNT_TYPE);
	}
	
	@Override
	public List<InterestRate> getAllInterestRate() {
		List<InterestRate> interestRates = interestRateDao.getAllInterestRates();
		return interestRates;
	}

	@Override
	public InterestRate getInterestRateByID(int interestRateID) {
		InterestRate interestRate = null;
		if(interestRateDao != null)
			interestRate = interestRateDao.getInterestRateByID(interestRateID);
		return interestRate;
	}
	
	@Override
	public List<Currency> getAllCurrency() {
		List<Currency> currencies = currencyDao.getAllCurrency();
		return currencies;
	}

	@Override
	public List<PeriodType> getAllPeriodTypes() {
		List<PeriodType> periodTypes = periodTypeDao.getAllPeriodTypes();
		return periodTypes;
	}

	@Override
	public double getInterestRate(int currencyID, int periodTypeID) {
		InterestRate interestRate = interestRateDao.getInterestRateByType(currencyID, periodTypeID);
		if(interestRate != null) return interestRate.getRate();
		return 0;
	}

	@Override
	public double calculateAfterBalance(double currentBalance, double funds,
			double interestRate, int periodTypeID, SavingTransactionType savingTransactionType) {
		int numberWeeks = getNumberOfWeeks(periodTypeID);
		System.out.println("rate/week: " + interestRate/48 + " weeks: " + numberWeeks);
		if (savingTransactionType == SavingTransactionType.SAVING) {
			return currentBalance + funds + funds*numberWeeks*interestRate/4800;
		}else if(savingTransactionType == SavingTransactionType.WITHDRAW){
			return currentBalance - funds;
		}
		return currentBalance;
	}


	@Override
	public List<InterestRate> getAllInterestRateByCurrencyID(int currencyID) {
		List<InterestRate> interest = interestRateDao.getAllInterestRateByCurrencyID(currencyID);
		return interest;
	}


	@Override
	public int getNumberOfWeeks(int periodTypeID) {
		PeriodType periodType = periodTypeDao.getPeriodTypeByID(periodTypeID);
		if(periodType!= null)
			return periodType.getSavingPeriod();
		return 0;
	}

	@Override
	public Timestamp getExpiryDate(int periodTypeID) {
		int numberWeeks = getNumberOfWeeks(periodTypeID);
		Timestamp date = new Timestamp(System.currentTimeMillis() + 604800000*numberWeeks); //7*24*60*60*1000
		return date;
	}


}
